/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author T.Ron
 */
public class EcriturecJpaController implements Serializable {

    public EcriturecJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public EcriturecJpaController() {
    }
    
    
    
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Ecriturec ecriturec) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Periode periode = ecriturec.getPeriode();
            if (periode != null) {
                periode = em.getReference(periode.getClass(), periode.getPeriodePK());
                ecriturec.setPeriode(periode);
            }
            Journaux numjo = ecriturec.getNumjo();
            if (numjo != null) {
                numjo = em.getReference(numjo.getClass(), numjo.getNumjo());
                ecriturec.setNumjo(numjo);
            }
            Joperiode idJop = ecriturec.getIdJop();
            if (idJop != null) {
                idJop = em.getReference(idJop.getClass(), idJop.getIdJop());
                ecriturec.setIdJop(idJop);
            }
            Comptet idCt = ecriturec.getIdCt();
            if (idCt != null) {
                idCt = em.getReference(idCt.getClass(), idCt.getIdCt());
                ecriturec.setIdCt(idCt);
            }
            Compteg idCg = ecriturec.getIdCg();
            if (idCg != null) {
                idCg = em.getReference(idCg.getClass(), idCg.getIdCg());
                ecriturec.setIdCg(idCg);
            }
            em.persist(ecriturec);
            if (periode != null) {
                periode.getEcriturecCollection().add(ecriturec);
                periode = em.merge(periode);
            }
            if (numjo != null) {
                numjo.getEcriturecCollection().add(ecriturec);
                numjo = em.merge(numjo);
            }
            if (idJop != null) {
                idJop.getEcriturecCollection().add(ecriturec);
                idJop = em.merge(idJop);
            }
            if (idCt != null) {
                idCt.getEcriturecCollection().add(ecriturec);
                idCt = em.merge(idCt);
            }
            if (idCg != null) {
                idCg.getEcriturecCollection().add(ecriturec);
                idCg = em.merge(idCg);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Ecriturec ecriturec) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ecriturec persistentEcriturec = em.find(Ecriturec.class, ecriturec.getIdEc());
            Periode periodeOld = persistentEcriturec.getPeriode();
            Periode periodeNew = ecriturec.getPeriode();
            Journaux numjoOld = persistentEcriturec.getNumjo();
            Journaux numjoNew = ecriturec.getNumjo();
            Joperiode idJopOld = persistentEcriturec.getIdJop();
            Joperiode idJopNew = ecriturec.getIdJop();
            Comptet idCtOld = persistentEcriturec.getIdCt();
            Comptet idCtNew = ecriturec.getIdCt();
            Compteg idCgOld = persistentEcriturec.getIdCg();
            Compteg idCgNew = ecriturec.getIdCg();
            if (periodeNew != null) {
                periodeNew = em.getReference(periodeNew.getClass(), periodeNew.getPeriodePK());
                ecriturec.setPeriode(periodeNew);
            }
            if (numjoNew != null) {
                numjoNew = em.getReference(numjoNew.getClass(), numjoNew.getNumjo());
                ecriturec.setNumjo(numjoNew);
            }
            if (idJopNew != null) {
                idJopNew = em.getReference(idJopNew.getClass(), idJopNew.getIdJop());
                ecriturec.setIdJop(idJopNew);
            }
            if (idCtNew != null) {
                idCtNew = em.getReference(idCtNew.getClass(), idCtNew.getIdCt());
                ecriturec.setIdCt(idCtNew);
            }
            if (idCgNew != null) {
                idCgNew = em.getReference(idCgNew.getClass(), idCgNew.getIdCg());
                ecriturec.setIdCg(idCgNew);
            }
            ecriturec = em.merge(ecriturec);
            if (periodeOld != null && !periodeOld.equals(periodeNew)) {
                periodeOld.getEcriturecCollection().remove(ecriturec);
                periodeOld = em.merge(periodeOld);
            }
            if (periodeNew != null && !periodeNew.equals(periodeOld)) {
                periodeNew.getEcriturecCollection().add(ecriturec);
                periodeNew = em.merge(periodeNew);
            }
            if (numjoOld != null && !numjoOld.equals(numjoNew)) {
                numjoOld.getEcriturecCollection().remove(ecriturec);
                numjoOld = em.merge(numjoOld);
            }
            if (numjoNew != null && !numjoNew.equals(numjoOld)) {
                numjoNew.getEcriturecCollection().add(ecriturec);
                numjoNew = em.merge(numjoNew);
            }
            if (idJopOld != null && !idJopOld.equals(idJopNew)) {
                idJopOld.getEcriturecCollection().remove(ecriturec);
                idJopOld = em.merge(idJopOld);
            }
            if (idJopNew != null && !idJopNew.equals(idJopOld)) {
                idJopNew.getEcriturecCollection().add(ecriturec);
                idJopNew = em.merge(idJopNew);
            }
            if (idCtOld != null && !idCtOld.equals(idCtNew)) {
                idCtOld.getEcriturecCollection().remove(ecriturec);
                idCtOld = em.merge(idCtOld);
            }
            if (idCtNew != null && !idCtNew.equals(idCtOld)) {
                idCtNew.getEcriturecCollection().add(ecriturec);
                idCtNew = em.merge(idCtNew);
            }
            if (idCgOld != null && !idCgOld.equals(idCgNew)) {
                idCgOld.getEcriturecCollection().remove(ecriturec);
                idCgOld = em.merge(idCgOld);
            }
            if (idCgNew != null && !idCgNew.equals(idCgOld)) {
                idCgNew.getEcriturecCollection().add(ecriturec);
                idCgNew = em.merge(idCgNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ecriturec.getIdEc();
                if (findEcriturec(id) == null) {
                    throw new NonexistentEntityException("The ecriturec with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ecriturec ecriturec;
            try {
                ecriturec = em.getReference(Ecriturec.class, id);
                ecriturec.getIdEc();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ecriturec with id " + id + " no longer exists.", enfe);
            }
            Periode periode = ecriturec.getPeriode();
            if (periode != null) {
                periode.getEcriturecCollection().remove(ecriturec);
                periode = em.merge(periode);
            }
            Journaux numjo = ecriturec.getNumjo();
            if (numjo != null) {
                numjo.getEcriturecCollection().remove(ecriturec);
                numjo = em.merge(numjo);
            }
            Joperiode idJop = ecriturec.getIdJop();
            if (idJop != null) {
                idJop.getEcriturecCollection().remove(ecriturec);
                idJop = em.merge(idJop);
            }
            Comptet idCt = ecriturec.getIdCt();
            if (idCt != null) {
                idCt.getEcriturecCollection().remove(ecriturec);
                idCt = em.merge(idCt);
            }
            Compteg idCg = ecriturec.getIdCg();
            if (idCg != null) {
                idCg.getEcriturecCollection().remove(ecriturec);
                idCg = em.merge(idCg);
            }
            em.remove(ecriturec);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ecriturec> findEcriturecEntities() {
        return findEcriturecEntities(true, -1, -1);
    }

    public List<Ecriturec> findEcriturecEntities(int maxResults, int firstResult) {
        return findEcriturecEntities(false, maxResults, firstResult);
    }

    private List<Ecriturec> findEcriturecEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Ecriturec.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Ecriturec findEcriturec(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ecriturec.class, id);
        } finally {
            em.close();
        }
    }

    public int getEcriturecCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Ecriturec> rt = cq.from(Ecriturec.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
